//
//  KDiscoveryRootViewController.m
//  DrugPin
//
//  Created by guohe on 2018/9/8.
//  Copyright © 2018年 outang. All rights reserved.
//
#import "KNavigationSelectView.h"
#import "KDiscoveryRootViewController.h"
#import "KDiscoveryTableViewCell.h"

//sType（1004:专业服务；1002：实战案例；1005：户外促销）
#define KProfessionalervices @"1004" //专业服务
#define KOutdoorPromotion @"1005" //户外促销
#define KPracticalCases @"1002" //实战案例

@interface KDiscoveryRootViewController ()<UITableViewDelegate,UITableViewDataSource>{
    KDiscoveryTableViewCell *cell; //封装的cell
}
// 导航栏
@property (nonatomic, strong)KNavigationSelectView *navigationSelectView;
// 底层tableView
@property (nonatomic, strong)UITableView *mainTable;
// mainTable 封装cell
@property (nonatomic, strong)KDiscoveryTableViewCell *DiscoveryCell;
@end

@implementation KDiscoveryRootViewController
#pragma mark - 生命周期
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [self setUpUI];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KRandomColor;
    //[self setUpUI];
    [self loadDataWithSType:KProfessionalervices];
}

#pragma mark - 通过传入不同类型进行不同页面的接口请求
-(void)loadDataWithSType:(NSString *)sType{
    [SVProgressHUD show];
    NSDictionary *parameters = @{
                                 @"_appid" : @"103",
                                 @"_code" : [KUserLoginModel shareInstance].Code ? [KUserLoginModel shareInstance].Code : [KWXLoginUserListModel shareInstance].Code,
                                 @"sType": sType, //接口请求类型区分
                                 @"Keyword":@"", //搜索框关键字
                                 @"PageIndex":@"1", //页码
                                 @"PageSize":@"30" //条数
                                 };
    


    self.discoveryDataListModelArray= [NSMutableArray array];

    [[NetworkSingleton sharedManager]POSTdata:parameters Path:@"activity/news/getnewslist" successBlock:^(id responseBody) {

        BaseModel *baseModel = [BaseModel mj_objectWithKeyValues:responseBody];
        if (baseModel.code != 0)
        {
            [SVProgressHUD showInfoWithStatus:baseModel.msg];
            [SVProgressHUD dismissWithDelay:1];
            return ;
        }

        [SVProgressHUD dismiss];
        //请求数据"data"转模型
        self.discoverModel = [KDiscoveryModel mj_objectWithKeyValues:[responseBody valueForKey:@"data"]];
        [self.discoveryDataListModelArray addObjectsFromArray:self.discoverModel.DataList];
        
        [self.mainTable reloadData];
    } failureBlock:^(NSString *error) {
        ErrorShow(error);
    }];
}

-(void)setUpUI{
    //添加到视图
    [self.view addSubview:self.navigationSelectView];
    [self.navigationSelectView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).mas_offset(20);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    
    [self.view addSubview:self.mainTable];
    [self.mainTable mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navigationSelectView.mas_bottom);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
    
    [self.navigationSelectView.redEnvelopeActivityBtn addTarget:self action:@selector(OnRedEnvelopeActivityBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationSelectView.cultivateActivityBtn addTarget:self action:@selector(onCultivateActivityBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationSelectView.scratchRedEnvelopeBtn addTarget:self action:@selector(onScratchRedEnvelopeBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.discoveryDataListModelArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell %zd %zd",indexPath.section,indexPath.row];
    
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[KDiscoveryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    [cell.contentImageView sd_setImageWithURL:[NSURL URLWithString:self.discoveryDataListModelArray[indexPath.row].Pic] placeholderImage:[UIImage imageNamed:@"loading"]];
    cell.mainTitleLabel.text = self.discoveryDataListModelArray[indexPath.row].Title;
    
    
    NSString *contentStr = [NSString stringWithFormat:@"%@      %@",
                            self.discoveryDataListModelArray[indexPath.row].CreateDateStr,
                            self.discoveryDataListModelArray[indexPath.row].TotalRead];
    
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
    // 创建一个文字附件对象
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:@"yuedu"];  //设置图片源
    textAttachment.bounds = CGRectMake(0, 0, 12, 10);  //设置图片位置和大小
    // 将文字附件转换成属性字符串
    NSAttributedString *attachmentAttrStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
    // 将转换成属性字符串插入到目标字符串
    [attrStr insertAttributedString:attachmentAttrStr atIndex:11];
    cell.subTitleLabel.attributedText = attrStr;
    return cell;
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 240;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BaseWebViewController *sub = [[BaseWebViewController alloc] init];
    NSString *urlString = [self.discoveryDataListModelArray[indexPath.row].web valueForKey:@"ClickUrl"];
    sub.urlStr = [NSString stringWithFormat:@"%@,%@",urlString,[KUserLoginModel shareInstance].Code ? [KUserLoginModel shareInstance].Code : [KWXLoginUserListModel shareInstance].Code];
    sub.title = [self.discoveryDataListModelArray[indexPath.row].web valueForKey:@"Title"];
    sub.hidesBottomBarWhenPushed = YES;
    [sub showCustomNavigationBackButton];
    [self.navigationController pushViewController:sub animated:YES];
}


#pragma mark - 按钮点击方法
-(void)OnRedEnvelopeActivityBtnClick{
    //改变选择颜色
    [self.navigationSelectView.redEnvelopeActivityBtn setTitleColor:KColorOneAlpha(48,138,245) forState:UIControlStateNormal];
    [self.navigationSelectView.cultivateActivityBtn setTitleColor:KColorOneAlpha(51,51,51) forState:UIControlStateNormal];
    [self.navigationSelectView.scratchRedEnvelopeBtn setTitleColor:KColorOneAlpha(51,51,51) forState:UIControlStateNormal];
    [self.navigationSelectView.bgLine mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.navigationSelectView.redEnvelopeActivityBtn.mas_bottom);
        make.left.right.mas_equalTo(self.navigationSelectView.redEnvelopeActivityBtn);
        make.height.mas_equalTo(2);
    }];
    
    //专业阅读
    [self loadDataWithSType:KProfessionalervices];
}

-(void)onCultivateActivityBtnClick{
    //改变选择颜色
    [self.navigationSelectView.redEnvelopeActivityBtn setTitleColor:KColorOneAlpha(51,51,51) forState:UIControlStateNormal];
    [self.navigationSelectView.cultivateActivityBtn setTitleColor:KColorOneAlpha(48,138,245) forState:UIControlStateNormal];
    [self.navigationSelectView.scratchRedEnvelopeBtn setTitleColor:KColorOneAlpha(51,51,51) forState:UIControlStateNormal];
    [self.navigationSelectView.bgLine mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.navigationSelectView.cultivateActivityBtn.mas_bottom);
        make.left.right.mas_equalTo(self.navigationSelectView.cultivateActivityBtn);
        make.height.mas_equalTo(2);
    }];
    
    //户外促销
    [self loadDataWithSType:KOutdoorPromotion];
}

-(void)onScratchRedEnvelopeBtnClick{
    //改变选择颜色
    [self.navigationSelectView.redEnvelopeActivityBtn setTitleColor:KColorOneAlpha(51,51,51) forState:UIControlStateNormal];
    [self.navigationSelectView.cultivateActivityBtn setTitleColor:KColorOneAlpha(51,51,51) forState:UIControlStateNormal];
    [self.navigationSelectView.scratchRedEnvelopeBtn setTitleColor:KColorOneAlpha(48,138,245) forState:UIControlStateNormal];
    
    [self.navigationSelectView.bgLine mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.navigationSelectView.scratchRedEnvelopeBtn.mas_bottom);
        make.left.right.mas_equalTo(self.navigationSelectView.scratchRedEnvelopeBtn);
        make.height.mas_equalTo(2);
    }];
    
    //实战案例
    [self loadDataWithSType:KPracticalCases];
}

#pragma mark - getter
-(KNavigationSelectView *)navigationSelectView{
    if (!_navigationSelectView) {
        _navigationSelectView = [[KNavigationSelectView alloc] initwithNaviType:KNaviType_Discovery];
        _navigationSelectView.backgroundColor = KWhiteColor;
    }
    return _navigationSelectView;
}

-(UITableView *)mainTable{
    if (!_mainTable) {
        _mainTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _mainTable.tableFooterView = [[UIView alloc] init];
        _mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTable.delegate = self;
        _mainTable.dataSource = self;
    }
    return _mainTable;
}


@end
